<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

$Id$

Notes:
--->
<cfcomponent
	displayname="SubscriberGateway"
	output="false"
	extends="SubscriberGateway"
	hint="SubscriberGateway for MachBlog (PostgreSQL)">

	<cffunction name="init" access="public" output="false" returntype="SubscriberGateway"
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>

	<cffunction name="getSubscribers" access="public" output="false" returntype="query"
			hint="Returns a query object containing blog subscribers, optionally based on the search terms passed in">
		<cfargument name="searchText" type="string" required="false" default="" />
		<cfargument name="activeOnly" type="boolean" required="false" default="false" />

		<cfset var getSubscribers = "" />

		<cftry>
			<cfquery name="getSubscribers" datasource="#getDatasource().getDsn()#"
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT *
				FROM machblog_subscriber
				WHERE 1 = 1
			<cfif arguments.searchText NEQ "">
				AND (subscriber_email LIKE '%#searchText#%'
					OR ip_created LIKE '%#searchText#%')
			</cfif>
			<cfif arguments.activeOnly>
				AND is_active = <cfqueryparam value="1" cfsqltype="cf_sql_smallint" />
			</cfif>
				ORDER BY dt_created DESC
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<cfreturn getSubscribers />
	</cffunction>

	<cffunction name="getSubscriptionCount" access="public" output="false" returntype="numeric"
			hint="Returns the number of blog subscribers">
		<cfset var getSubscriptionCount = "" />
		<cfset var subscriptionCount = 0 />

		<cftry>
			<cfquery name="getSubscriptionCount" datasource="#getDatasource().getDsn()#"
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT COUNT(subscriber_email) AS subscription_count
				FROM machblog_subscriber
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<cfif IsQuery(getSubscriptionCount) AND getSubscriptionCount.RecordCount GT 0>
			<cfset subscriptionCount = getSubscriptionCount.subscription_count />
		</cfif>

		<cfreturn subscriptionCount />
	</cffunction>

	<cffunction name="verifyBlogSubscription" access="public" output="false" returntype="boolean" 
			hint="Verifies a pending blog subscriptin and returns a boolean indicating whether or not the verification was successful">
		<cfargument name="email" type="string" required="true" />
		<cfargument name="verifyId" type="uuid" required="true" />
		
		<cfset var getSubscriberId = 0 />
		<cfset var validateSubscriber = 0 />
		<cfset var success = false />
		
		<cfquery name="getSubscriberId" datasource="#getDatasource().getDsn()#" 
				username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
			SELECT 	subscriber_id 
			FROM 	machblog_subscriber 
			WHERE  	subscriber_email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="100" /> 
			AND 	verify_id = <cfqueryparam value="#arguments.verifyId#" cfsqltype="cf_sql_char" maxlength="35" />
		</cfquery>
		
		<cfif getSubscriberId.recordCount eq 1>
			<cftry>
				<cfquery name="validateSubscriber" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
					UPDATE 	machblog_subscriber 
					SET 	is_active = <cfqueryparam value="1" cfsqltype="cf_sql_smallint" /> 
					WHERE 	subscriber_id = <cfqueryparam value="#getSubscriberId.subscriber_id#" cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				
				<cfset success = true />
				
				<cfcatch type="any">
					<cfset success = false />
				</cfcatch>
			</cftry>
		</cfif>
		
		<cfreturn success />
	</cffunction>
</cfcomponent>